/*******************************************************************************
**
** Copyright (c) 2011 - The OWE Team
**
** This file is part of OWE.
**
** OWE is a free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; version
** version 2.1 of the License.
**
** OWE is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with OWE. If not, see <http://www.gnu.org/licenses/>.
**
*******************************************************************************/

#include <owe/featureelement.h>
#include <owe/node.h>
#include <owe/paramelement.h>
#include <owe/ustring.h>

OWE_USE_NAMESPACE

/*!
    \class FeatureElement
    \brief Special resources needed by the widget.

    \ingroup Configuration

    Implementation of the widget feature element as specified
    <a href="http://www.w3.org/TR/widgets/#feature">here</a>.
*/

/*!
    \fn UString FeatureElement::name() const

    Returns the feature <a href="http://www.w3.org/TR/widgets/#name0">name</a>.
    This attribute is mandatory and features without name are considered
    invalid.
*/

/*!
    \fn UString FeatureElement::required() const

    Returns if feature is <a href="http://www.w3.org/TR/widgets/#required">
    required</a>. If the widget is unable to access this resource (because of
    security policies or any other reason, the user agent should invalidate the
    widget as a whole.
*/

/*!
    \fn const ParamElementList FeatureElement::param() const

    Return the list of custom parameters of this feature. A parameter it's a way
    to customize the feature and it's not mandatory.
*/

/*!
    Constructs a new FeatureElement from a given \a node.
*/
FeatureElement::FeatureElement(const Node *node)
    : Element(node, Element::Feature)
    , m_name(node->attribute("name"))
    , m_required(node->attribute("required"))
{
    if (m_required.size() == 0)
        m_required = "true";
}

/*!
    Destructs the object releasing all allocated resources.
*/
FeatureElement::~FeatureElement()
{
    std::map<UString, ParamElement *>::iterator it;

    for (it = m_param.begin(); it != m_param.end(); it++)
        delete it->second;

    m_param.clear();
}

bool FeatureElement::addParam(ParamElement *param)
{
    if (!param->isValid() || m_param.find(param->name()) != m_param.end())
        return Element::addChild(param);

    m_param[param->name()] = param;

    return true;
}

bool FeatureElement::addChild(Element *element)
{
    if (element->type() == Element::Param)
        return addParam(static_cast<ParamElement *>(element));

    return Element::addChild(element);
}

/*!
    Returns whenever a feature is considered valid. To be valid, the feature
    must have at least the name attribute. Features without the required
    attribute, have it set to true by default.
*/
bool FeatureElement::isValid() const
{
    return m_name.size() > 0;
}
